package com.worksmap.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.worksmap.common.GeneralDao;
import com.worksmap.core.dao.UserDao;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.entity.User;
import com.worksmap.core.util.ObjectUtil;
import com.worksmap.core.util.StringUtil;

@Repository
public class UserDaoImpl extends GeneralDao implements UserDao {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	public User getUserById(int userId) {
		User user = null;
		try {
			user = this.getEntity(User.class, userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public User getUserByName(String userName) {
		if(StringUtil.isEmpty(userName)){
			return null;
		}
		User user = null;
		try {
			user = this.getEntity(User.class, " select * from user where userName = ? ", new Object[]{userName});
		} catch (Exception e) {
			logger.error("通过用户名获取用户错误！" + e);
		}
		return user;
	}

	@Override
	public List<User> getUserListByCondition(User user) {
		List<User> userList = null;
		List<Object> valueList = new ArrayList<Object>();
		StringBuilder sqlStr = new StringBuilder(" select * from user where 1 = 1 ");
		if(user != null){
			if(StringUtil.isNotBlank(user.getUserName())){
				valueList.add(user.getUserName());
			}
			if(user.getUserRole() > 0){
				valueList.add(user.getUserRole());
			}
		}
		try {
			userList = this.getEntityList(User.class, sqlStr.toString() , valueList.toArray());
		} catch (Exception e) {
			logger.error("查询用户列表错误！" + e);
		}
		return userList;
	}

	@Override
	public boolean userRegister(User user) {
		try {
			return this.saveEntity("INSERT INTO USER (userName,passWord,userRole,email) " +
					"values(:userName,:passWord,:userRole,:email)", 
					user);
		} catch (Exception e) {
			logger.error("保存用户错误！" + e);
		}
		return false;
	}

	@Override
	public boolean updateUser(User user, Object... fieldName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delUserById(int userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean saveSeller(TaoBaoSeller seller) {
		try {
			return this.saveEntity("INSERT INTO SELLER ( " + ObjectUtil.getClassFieldStr(TaoBaoSeller.class) + ")" +
					"values(:taobao_user_id,:taobao_user_nick,:access_token,:token_type,:expires_in," +
					":refresh_token,:re_expires_in,:r1_expires_in,:r2_expires_in,:w1_expires_in,:w2_expires_in)", 
					seller);
			
		} catch (Exception e) {
			logger.error("保存用户错误！" + e);
		}
		return false;
	}

	@Override
	public boolean updateSeller(TaoBaoSeller seller) {
		if(seller == null){
			return false;
		}
		try {
			return this.updateEntity(seller, 
					new String[]{"access_token", "expires_in", "refresh_token", 
					"re_expires_in", "r1_expires_in", "r2_expires_in", "w1_expires_in", "w2_expires_in"});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	@Override
	public TaoBaoSeller getSellerById(String sellerId) {
		if(StringUtil.isEmpty(sellerId)){
			return null;
		}
		TaoBaoSeller user = null;
		try {
			user = this.getEntity(TaoBaoSeller.class, " select * from SELLER where taobao_user_id = ? ", new Object[]{sellerId});
		} catch (Exception e) {
			logger.error("通过sellerId获取用户错误！" + e);
		}
		return user;
	}

	@Override
	public TaoBaoSeller getSellerByNick(String nick) {
		if(StringUtil.isEmpty(nick)){
			return null;
		}
		TaoBaoSeller user = null;
		try {
			user = this.getEntity(TaoBaoSeller.class, " select * from SELLER where taobao_user_nick = ? ", new Object[]{nick});
		} catch (Exception e) {
			logger.error("通过nick获取用户错误！" + e);
		}
		return user;
	}

}
